<?php

namespace ticket\modules\backend\models;

use Yii;

/**
 * This is the model class for table "tk_ticket_info".
 *
 * @property int $tid
 * @property int $aid
 * @property int $app_id
 * @property string $name
 * @property int $type_id
 * @property string $is_advance
 * @property string $ticket_sn
 * @property string $ori_price
 * @property string $category
 * @property string $ruling_price
 * @property string $state
 * @property int $sort
 * @property string $create_time
 * @property string $update_time
 */
class TicketInfo extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'tk_ticket_info';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            ['name','required','message' => '门票名称不能为空'],
            ['ticket_sn','required','message' => '票码不能为空'],
            ['ori_price','required','message' => '原价不能为空'],
            ['ruling_price','required','message' => '现价不能为空'],
            ['app_id','required','message' => '实例id不能为空'],
            [['aid', 'type_id', 'sort'], 'integer','message' => '排序字段格式有误'],
            [['is_advance', 'state','now_ticket_sn'], 'string'],
            [['ori_price', 'ruling_price','common_proportion','member_proportion'], 'number'],
            [['create_time', 'update_time','category','now_ticket_price','vip_price'], 'safe'],
            [['name', 'ticket_sn','vip_ticket_sn'], 'string', 'max' => 64],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'tid' => 'Tid',
            'aid' => 'Aid',
            'app_id' => 'App Id',
            'name' => 'Name',
            'type_id' => 'Type ID',
            'is_advance' => 'Is Advance',
            'ticket_sn' => 'Ticket Sn',
            'ori_price' => 'Ori Price',
            'ruling_price' => 'Ruling Price',
            'state' => 'State',
            'now_ticket_sn' => 'now_ticket_sn',
            'now_ticket_price' => 'now_ticket_price',
            'sort' => 'Sort',
            'create_time' => 'Create Time',
            'update_time' => 'Update Time',
        ];
    }


    /*** 添加门票信息Model
     * @param $aid
     * @param $params
     * @return bool|mixed
     */
    public function addTicketInfo($app_id,$params)
    {
        $data = $this->setData($params);
        $data['TicketInfo']['app_id'] = $app_id;
        $aid = Application::find()->where(['app_id' => $app_id])->select(['aid'])->asArray()->one()['aid'];
        if (empty($aid)){
            return $this->addError('aid','请先创建应用');
        }
        // 票码存在 不能添加
//       20210425-取消一个票码对应一张票，且不能重复
//        $ticketSn = self::find()->where('ticket_sn = :ticketsn',[':ticketsn' => $data['TicketInfo']['ticket_sn']])->one();
//        if (!is_null($ticketSn)){
//            return false;
//        }
        $data['TicketInfo']['aid'] = $aid;
        if ($this->load($data) and $this->validate()){
            if ($this->save(0)){
                $tid = $this->getPrimaryKey();
                return $tid;
            }
        }
        return false;
    }

    /*** 修改门票数据
     * @param $tid
     * @param $params
     * @return bool
     */
    public function modTicketInfo($tid,$app_id,$params)
    {
        $params = $params['ticketInfo'];
        $data = $this->setData($params);
        $data['TicketInfo']['app_id'] = $app_id;
        if ($this->load($data) and $this->validate()){
            $model = self::find()->where('tid = :id',[':id' => $tid])->one();
            if (is_null($model)){
                return false;
            }
            $model->name = $data['TicketInfo']['name'];
            $model->type_id = $data['TicketInfo']['type_id'];
            $model->ticket_sn = $data['TicketInfo']['ticket_sn'];
            $model->ori_price = $data['TicketInfo']['ori_price'];
            $model->ruling_price = $data['TicketInfo']['ruling_price'];
            $model->state = $data['TicketInfo']['status'];
            $model->now_ticket_sn = $data['TicketInfo']['now_ticket_sn'];
            $model->now_ticket_price = $data['TicketInfo']['now_ticket_price'];
            $model->sort = $data['TicketInfo']['sort'];
            $model->update_time = $data['TicketInfo']['update_time'];
            $model->category = $data['TicketInfo']['category'];
            $model->common_proportion = $data['TicketInfo']['common_proportion'];
            $model->member_proportion = $data['TicketInfo']['member_proportion'];
			$model->vip_ticket_sn = $data['TicketInfo']['vip_ticket_sn'];
			$model->vip_price = $data['TicketInfo']['vip_price'];
            if ($model->save(0)){
                return true;
            }
        }
        return false;

    }

    /*** 获取单个门票数据
     * @param $tid
     * @return \yii\db\DataReader
     * @throws \yii\db\Exception
     */
    public function getTicketInfo($tid)
    {

        $sql = "select * from tk_ticket_info a left join tk_ticket_additional b on a.tid = b.tid  where a.tid = {$tid}";
        $ticketInfo = Yii::$app->db->createCommand($sql)->queryOne();
        return $ticketInfo;
    }

    /*** 删除门票数据
     * @param $tid
     */
    public function delTicketInfo($tid)
    {
        $model = self::find()->where('tid = :id',[':id' => $tid])->one();
        if (!is_null($model)){
           if ($model->delete()){
               return true;
           }
        }
        return false;

    }


    /*** 根据应用获取门票id和名称
     * @param $aid
     * @return array|\yii\db\ActiveRecord[]
     */
    public function getTicketList($aid)
    {
        $data = self::find()->where('aid = :id',[':id' => $aid])->select(['tid','aid','name'])->asArray()->all();
        return $data;
    }


    /*** 门票所属分类
     * @return \yii\db\ActiveQuery
     */
    public function getType()
    {
        $data = $this->hasOne(TicketType::className(),['id' => 'type_id'])->select(['id','type_name']);
        return $data;
    }




    /*** 设置参数
     * @param $params
     * @return mixed
     */
    private function setData($params)
    {
        if (isset($params['name']) and !empty($params['name'])){
            $data['TicketInfo']['name'] = $params['name'];
        }else{
            $data['TicketInfo']['name'] = '';
        }
        if (isset($params['type_id']) and !empty($params['type_id'])){
            $data['TicketInfo']['type_id'] = $params['type_id'];
        }else{
            $data['TicketInfo']['type_id'] = 0;
        }
        if (isset($params['type_id']) and !empty($params['type_id'])){
            $data['TicketInfo']['category'] = $params['type_id'];
        }else{
            $data['TicketInfo']['category'] = 0;
        }
        if (isset($params['ticket_sn']) and !empty($params['ticket_sn'])){
            $data['TicketInfo']['ticket_sn'] = $params['ticket_sn'];
        }else{
            $data['TicketInfo']['ticket_sn'] = '';
        }
        if (isset($params['ori_price']) and !empty($params['ori_price'])){
            $data['TicketInfo']['ori_price'] = $params['ori_price'];
        }else{
            $data['TicketInfo']['ori_price'] = '';
        }
		
		if (isset($params['vip_ticket_sn']) and !empty($params['vip_ticket_sn'])){
		    $data['TicketInfo']['vip_ticket_sn'] = $params['vip_ticket_sn'];
		}else{
		    $data['TicketInfo']['vip_ticket_sn'] = '';
		}
		if (isset($params['vip_price']) and !empty($params['vip_price'])){
		    $data['TicketInfo']['vip_price'] = $params['vip_price'];
		}else{
		    $data['TicketInfo']['vip_price'] = '';
		}
		
		
        if (isset($params['ruling_price']) and !empty($params['ruling_price'])){
            $data['TicketInfo']['ruling_price'] = $params['ruling_price'];
        }else{
            $data['TicketInfo']['ruling_price'] = '';
        }
        if (isset($params['state']) and !empty($params['state'])){
            $data['TicketInfo']['status'] = $params['state'];
        }else{
            $data['TicketInfo']['status'] = '1';
        }
        if (isset($params['sort']) and !empty($params['sort'])){
            $data['TicketInfo']['sort'] = $params['sort'];
        }else{
            $data['TicketInfo']['sort'] = 0;
        }
        if (isset($params['now_ticket_sn']) and !empty($params['now_ticket_sn'])){
            $data['TicketInfo']['now_ticket_sn'] = $params['now_ticket_sn'];
        }else{
            $data['TicketInfo']['now_ticket_sn'] = '';
        }
        if (isset($params['now_ticket_price']) and !empty($params['now_ticket_price'])){
            $data['TicketInfo']['now_ticket_price'] = $params['now_ticket_price'];
        }else{
            $data['TicketInfo']['now_ticket_price'] = '';
        }
        if (isset($params['aid']) and !empty($params['aid'])){
            $data['TicketInfo']['aid'] = $params['aid'];
        }else{
            $data['TicketInfo']['aid'] = '';
        }
        $data['TicketInfo']['create_time'] = date("Y-m-d H:i:s");
        $data['TicketInfo']['update_time'] = date("Y-m-d H:i:s");
        
        if (isset($params['common_proportion']) and !empty($params['common_proportion'])){
            $data['TicketInfo']['common_proportion'] = $params['common_proportion'];
        }else{
            $data['TicketInfo']['common_proportion'] = '';
        }
         if (isset($params['member_proportion']) and !empty($params['member_proportion'])){
            $data['TicketInfo']['member_proportion'] = $params['member_proportion'];
        }else{
            $data['TicketInfo']['member_proportion'] = '';
        }
        return $data;
    }
    
    
    
}
